
#include "member_BSlider.h"
#include "member_BWidget.h"

using namespace BWE;

member_BSlider::member_BSlider(BSlider* slider)
{
	boss = slider;
	value = 0;
	step = 1;
	readOnly = false;
	minimum = 0;
	maximum = 100;
	orientation = Orientation_Horizontal;
	hovered = false;
	pressed = false;
	handleGraph = Graph_Square_Fill;
	grooveSize = 3;
	dirty = true;
}
member_BSlider::~member_BSlider()
{

}

int member_BSlider::valueOfPoint(const BPoint& pos) const
{
	float value = 0;
	if (orientation & Orientation_Horizontal)
	{
		if (orientation & Orientation_Reverse)
			value = (float)(grooveRect.right() - pos.x()) / grooveRect.width() * (maximum - minimum);
		else
			value = (float)(pos.x() - grooveRect.left()) / grooveRect.width() * (maximum - minimum);
	}
	if (orientation & Orientation_Vertical)
	{
		if (orientation & Orientation_Reverse)
			value = (float)(pos.y() - grooveRect.top()) / grooveRect.height() * (maximum - minimum);
		else
			value = (float)(grooveRect.bottom() - pos.y()) / grooveRect.height() * (maximum - minimum);
	}
	value += minimum;
	value = bClamp(value, minimum, maximum);
	return (int)value;
}
void member_BSlider::freshRects()
{
	const BRect& crect = boss->centerRect();
	BPoint center = crect.center();
	int v = value - minimum;
	float range = (float)(maximum - minimum);
	if (orientation & Orientation_Horizontal)
	{
		int handleWidth = handleSize.width();
		if (handleWidth < 1)
			handleWidth = crect.height();
		int handleHeight = handleSize.height();
		if (handleHeight < 1)
			handleHeight = crect.height();

		int halfHandleWidth = handleWidth / 2;
		int halfGrooveSize = grooveSize / 2;
		grooveRect.left() = crect.left() + halfHandleWidth;
		grooveRect.top() = center.y() - halfGrooveSize;
		grooveRect.bottom() = center.y() + halfGrooveSize;
		grooveRect.right() = crect.right() - halfHandleWidth;

		int x = (int)(grooveRect.left() + (grooveRect.width() * v / range));
		if (orientation & Orientation_Reverse)
			x = (int)(grooveRect.right() - (grooveRect.width() * v / range));
		handleRect.setSize(handleWidth, handleHeight);
		handleRect.setCenter(x, center.y());
	}
	if (orientation & Orientation_Vertical)
	{
		int handleWidth = handleSize.width();
		if (handleWidth < 1)
			handleWidth = crect.width();
		int handleHeight = handleSize.height();
		if (handleHeight < 1)
			handleHeight = crect.width();

		int halfHandleWidth = handleWidth / 2;
		int halfGrooveSize = grooveSize / 2;
		grooveRect.left() = center.x() - halfGrooveSize;
		grooveRect.top() = crect.top() + halfHandleWidth;
		grooveRect.bottom() = crect.bottom() - halfHandleWidth;
		grooveRect.right() = center.x() + halfGrooveSize;

		int y = (int)(grooveRect.bottom() - (grooveRect.height() * v / range));
		if (orientation & Orientation_Reverse)
			y = (int)(grooveRect.top() + (grooveRect.height() * v / range));
		handleRect.setSize(handleWidth, handleHeight);
		handleRect.setCenter(center.x(), y);
	}

	if (IRound* round = widget_member(boss)->round)
	{
		handlePoints.reset();
		round->createInnerPoints(crect, handleRect, handlePoints);
		groovePoints.reset();
		round->createInnerPoints(crect, grooveRect, groovePoints);
	}
	else
	{
		handlePoints.clear();
		groovePoints.clear();
	}

}
